package com.itheima.advice;

import com.itheima.service.LogService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class Moneyadvice {
    @Autowired
    private LogService logService;

    @Pointcut(value = "execution(* com.itheima.service.AccountService.transfer(..))")
    private void pt() {
    }

    @Around("pt()")
    void moneyadvice(ProceedingJoinPoint jp) {
        Object[] args = jp.getArgs();
        String out = args[0].toString();
        String in = args[1].toString();
        Double money = Double.parseDouble(args[2].toString());
        try {
            jp.proceed();
            String info = "转账成功";
            logService.log(out, in, money);

        } catch (Throwable e) {
            String message = e.getMessage();
            logService.log(out, in, money, message);
        }

    }
}
